package cn.sinata.wanglin.mine.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.adapter.MyAreaAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.MyVillage;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.property.activity.OwnerCertificationActivity;

public class MyAreaActivity extends RecyclerListActivity {

    private MyAreaAdapter myAreaAdapter;
    private List<MyVillage> mData = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        setTitle("我的小区");
        addRightButton("添加小区", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(mContext).go(OwnerCertificationActivity.class).put("type", 1)
                        .start();
            }
        });
        setLoadMoreText("暂无更多数据");
        showDialog();
        getData();
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        myAreaAdapter = new MyAreaAdapter(mData);
        return myAreaAdapter;
    }

    void getData() {
        String userid = SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getMyVillage(userid)
                .subscribe(new ResultDataSubscriber<ArrayList<MyVillage>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<MyVillage> myvillage) {
                        mData.clear();
                        mData.addAll(myvillage);
                        myAreaAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void pullDownRefresh() {
        getData();
    }

    @Override
    protected void loadMore() {
        setRefreshing(false);
    }

    public void refresh() {
        pullDownRefresh();
    }
}
